package flight;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.*;
import java.util.StringTokenizer;
import javax.servlet.RequestDispatcher;

/**
 *
 * @author wewang
 */
public class FlightSearchServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String fromWhere = request.getParameter("fromwhere");
        String toWhere = request.getParameter("towhere");
        String departDate = request.getParameter("departdate");
        String returnDate = request.getParameter("returndate");
        String departTime = request.getParameter("departtime");
        String returnTime = request.getParameter("returntime");
        String noOfPassengers = request.getParameter("noofpassengers");
        String seatingClass = request.getParameter("seatingclass");

        String searchCriteria = "";
        String pageNum = null;
        
        FlightBean fb = new FlightBean();
        StringTokenizer st = null;
        fb.setDepartAirport(fromWhere);
        fb.setDepartCity(fromWhere);
        fb.setArriveAirport(toWhere);
        fb.setArriveCity(toWhere);
        
        searchCriteria += "From: " + fromWhere + "; To: " + toWhere + "; ";
        
        st = new StringTokenizer(departDate,"-");
        fb.setDepartMonth(Integer.parseInt(st.nextToken()));
        fb.setDepartDay(Integer.parseInt(st.nextToken()));
        fb.setDepartYear(Integer.parseInt(st.nextToken()));
        if (departTime.equals("0")) {
            fb.setDepartHourLower(0);
            fb.setDepartHourUpper(24);
        }
        else if (departTime.equals("1")) {
            fb.setDepartHourLower(0);
            fb.setDepartHourUpper(6);
        }
        else if (departTime.equals("2")) {
            fb.setDepartHourLower(6);
            fb.setDepartHourUpper(12);
        }
        else if (departTime.equals("3")) {
            fb.setDepartHourLower(12);
            fb.setDepartHourUpper(18);
        }
        else if (departTime.equals("4")) {
            fb.setDepartHourLower(18);
            fb.setDepartHourUpper(24);
        }
        
        searchCriteria += "Depart date: " + fb.getDepartYear() + "-" + fb.getDepartMonth() + "-" + fb.getDepartDay() + "; ";
        searchCriteria += "Depart time: " + fb.getDepartHourLower() + ":00:00" +  " to " + fb.getDepartHourUpper() + ":00:00" + "; ";

        st = new StringTokenizer(returnDate, "-");
        fb.setReturnMonth(Integer.parseInt(st.nextToken()));
        fb.setReturnDay(Integer.parseInt(st.nextToken()));
        fb.setReturnYear(Integer.parseInt(st.nextToken()));
        if (returnTime.equals("0")) {
            fb.setReturnHourLower(0);
            fb.setReturnHourUpper(24);
        }
        else if (returnTime.equals("1")) {
            fb.setReturnHourLower(0);
            fb.setReturnHourUpper(6);
        }
        else if (returnTime.equals("2")) {
            fb.setReturnHourLower(6);
            fb.setReturnHourUpper(12);
        }
        else if (returnTime.equals("3")) {
            fb.setReturnHourLower(12);
            fb.setReturnHourUpper(18);
        }
        else if (returnTime.equals("4")) {
            fb.setReturnHourLower(18);
            fb.setReturnHourUpper(24);
        }
        
        searchCriteria += "Return date: " + fb.getReturnYear() + "-" + fb.getReturnMonth() + "-" + fb.getReturnDay() + "; ";
        searchCriteria += "Return time: " + fb.getReturnHourLower() + ":00:00" +  " to " + fb.getReturnHourUpper() + ":00:00" + "; ";

        if (Integer.parseInt(seatingClass) == 1)
            fb.setSeatingClass("Coach");
        else if (Integer.parseInt(seatingClass) == 2)
            fb.setSeatingClass("Bussiness");
        else
            fb.setSeatingClass("First");
        
        searchCriteria += "Seating: " + fb.getSeatingClass() + "; ";
        
        fb.setNoOfTickets(Integer.parseInt(noOfPassengers));

        searchCriteria += "Number of Passengers: " + fb.getNoOfTickets() + "; ";
        
        fb.setNonStop(false);
        fb.setShowRoundabout(false);
        fb.setFuzzyLeaveDate(false);
        fb.setFuzzyReturnDate(false);
        fb.setSpecifiedAirline(false);
        fb.setSearchCriteria(searchCriteria);
        fb.setTripType(2);

        try {
            FlightPageBean fpb = new FlightPageBean();
            pageNum = (String)request.getParameter("jumpPage");
            if (pageNum == null)
                pageNum = "1";
            fpb.setFb(fb);          // set searching information bean
            fpb.setTripType(2);     // set trip type
            fpb.setCurrentType(1);
            PageBean pageCtl = fpb.listData(pageNum);
            request.setAttribute("pageCtl",pageCtl); // store PageBean object in request
            request.setAttribute("fb",fb);
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        RequestDispatcher dis = request.getRequestDispatcher("/rightpanels/results_flight_right.jsp");
        dis.forward(request,response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}